package com.lottery.system.dao.mapper;

import com.lottery.system.dao.dataobject.WinningRecordDO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author: xiaoxie
 * create: 2024-09-13 10:42
 * @BelongsProject: lottery-system
 * @BelongsPackage: com.lottery.system.dao.mapper
 * description: 中奖记录数据层接口
 */
@Mapper
public interface WinningRecordMapper {
    /**
     * @description: 批量保存中奖记录
     * @author: xiaoxie
     * @date: 2024/9/13 11:10
     * @param: [wRecordList]
     * @return: void
     **/
    @Insert("<script>" +
            " insert into winning_record (activity_id, activity_name" +
            " ,prize_id,prize_name,prize_tier" +
            " , winner_email,winner_id,winner_name,winner_phone_number,winning_time)" +
            " values <foreach collection = 'items' item='item' index='index' separator=','>" +
            " (#{item.activityId}, #{item.activityName}, #{item.prizeId}" +
            " , #{item.prizeName}, #{item.prizeTier},#{item.winnerEmail},#{item.winnerId} " +
            " , #{item.winnerName} ,#{item.winnerPhone},#{item.winningTime})" +
            " </foreach>" +
            " </script>")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void batchInsert(@Param(value = "items") List<WinningRecordDO> wRecordList);

    /**
     * @description: 通过活动id查询所有的中奖记录
     * @author: xiaoxie
     * @date: 2024/9/13 11:39
     * @param: [activityId]
     * @return: java.util.List<com.lottery.system.dao.dataobject.WinningRecordDO>
     **/
    @Select("select activity_id,activity_name,prize_id,prize_name,prize_tier" +
            " ,winner_email,winner_id,winner_name,winner_phone_number,winning_time " +
            " from winning_record " +
            " where activity_id = #{activityId} ")
    List<WinningRecordDO> selectById(@Param(value = "activityId") Long activityId);

    /**
     * @description: 通过活动id和奖品i查询记录数据条数
     * @author: xiaoxie
     * @date: 2024/9/15 21:37
     * @param: [activityId, prizeId]
     * @return: int
     **/
    @Select("select count(1) from winning_record " +
            " where activity_id = #{activityId} " +
            " and prize_id = #{prizeId} ")
    int countActivityAndPrizeId(@Param(value = "activityId") Long activityId,
                                @Param(value = "prizeId") Long prizeId);

    /**
     * @description: 动态SQL(奖品id可以为空,可以只单独删除活动id的记录)删除中奖记录
     * @author: xiaoxie
     * @date: 2024/9/15 21:44
     * @param: [activityId, prizeId 可以为空 ]
     * @return: void
     **/
    @Delete("<script>" +
            " delete from winning_record " +
            " where activity_id = #{activityId} " +
            " <if test=\"prizeId != null\"> " +
            "     and prize_id = #{prizeId} " +
            " </if>" +
            "</script>")
    void delete(@Param(value = "activityId") Long activityId,@Param(value = "prizeId") Long prizeId);

    /**
     * @description: 动态SQL(奖品id可以为空,如果为空就查询活动维度的记录,不为空就查询奖品维度的记录)
     *               查询中奖记录
     * @author: xiaoxie
     * @date: 2024/9/16 19:35
     * @param: [activityId, prizeId 可以为空]
     * @return: java.util.List<com.lottery.system.dao.dataobject.WinningRecordDO>
     **/
    @Select("<script>" +
            " select activity_id, activity_name,prize_id,prize_name" +
            " ,prize_tier,winner_email,winner_id,winner_name,winner_phone_number,winning_time " +
            " from winning_record " +
            " where activity_id = #{activityId} " +
            " <if test=\"prizeId != null\"> " +
            "     and prize_id = #{prizeId} " +
            " </if>" +
            "</script>")
    List<WinningRecordDO> selectByAOrPId(@Param(value = "activityId") Long activityId
                                        ,@Param(value = "prizeId") Long prizeId);
}
